//
//  PingPlugin.m
//  BSLPlugin
//
//  Created by Foreveross BSL on 16/1/7.
//  Copyright © 2016年 Fanty. All rights reserved.
//

#import "PingPlugin.h"
#import "Pingpp.h"
#import "AppDelegate.h"

@implementation PingPlugin

-(void)pluginInitialize{
    [super pluginInitialize];
    
    NSDictionary* dictonary=[NSDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"PingppConfig" withExtension:@"plist"]];

    scheme = [dictonary objectForKey:@"pingpp_scheme"];

}

-(void)setDebugMode:(CDVInvokedUrlCommand*)command{
    BOOL debug=NO;
    if([command.arguments count]>0){
        debug=[[command.arguments firstObject] isEqualToString:@"true"];
    }
    [Pingpp setDebugMode:debug];
    
    CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

- (void)createPayment:(CDVInvokedUrlCommand*)command{
    __block NSString* callbackId=command.callbackId;
    if([command.arguments count]>0){
        
        __weak PingPlugin* plugin=self;
        
        NSString* json=[command.arguments firstObject];
        [Pingpp createPayment:json viewController:self.viewController appURLScheme:scheme withCompletion:^(NSString *result, PingppError *error){
            if ([result isEqualToString:@"success"]) {
                // 支付成功
                CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
                [plugin.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
            } else {
                // 支付失败或取消
                NSLog(@"Error: code=%d msg=%@", error.code, [error getMsg]);
                CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error getMsg]];
                [plugin.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
            }
        }];

    }
    else{
        CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

    }
}

- (void)createPaymentWithWeixinOrAlipay:(CDVInvokedUrlCommand*)command{
    __block NSString* callbackId=command.callbackId;
    if([command.arguments count]>0){
        
        __weak PingPlugin* plugin=self;
        NSString* json=[command.arguments firstObject];
        
        [Pingpp createPayment:json appURLScheme:scheme withCompletion:^(NSString *result, PingppError *error){
            
            if ([result isEqualToString:@"success"]) {
                // 支付成功
                CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
                [plugin.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
                
            } else {
                // 支付失败或取消
                NSLog(@"Error: code=%d msg=%@", error.code, [error getMsg]);
                
                CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error getMsg]];
                [plugin.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
            }
        }];
        
    }
    else{
        CDVPluginResult*pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
        
    }
    
}

- (void)handleOpenURL:(NSNotification *)notification{
    NSURL* url = [notification object];
    if ([url isKindOfClass:[NSURL class]] && [url.scheme isEqualToString:scheme]){
        [Pingpp handleOpenURL:url withCompletion:nil];
    }
}




@end


